<?php

namespace App\Http\Controllers\Api;

use App\Model\SmsCodeTable;
use App\Model\UserTable;
use App\Repository\BaseController\ApiController;
use App\Repository\Sys\File\Uploader;
use App\Repository\Sys\JsonRespose;
use App\Repository\Sys\Sys;
use Illuminate\Support\Facades\Request;

/**
 *
 * 前后端公共接口
 * Class CommonController
 * @package App\Http\Controllers\Api
 */
class CommonController extends ApiController
{
    /**
     * @api {get} /api/common/login-out 退出登录接口
     * @apiGroup Wap-User
     * @apiDescription  清理cookie操作,前后台通用
     * @apiSuccessExample {json} Success-Response:
     *  {
     *    "code": 1,
     *    "msg": "成功",
     *    "data": {}
     *  }
     *
     */
    public function getLoginOut(){
        $uid=Sys::getUid();

        if($uid){
            UserTable::ORM()->where('id',$uid)->update(['open_id'=>'']);
        }
        \Session::flush();
        return JsonRespose::success();
    }

    /**
     * @api {post} /api/common/upload-img 通用上传图片接口
     * @apiGroup Wap-Common
     * @apiDescription  说明: 无
     *
     * @apiParam {File} upfile 文件接收字段
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     *   // ..... 文件地址返回
     * }
     * }
     */
    public function postUploadImg()
    {
        $CONFIG = \Config::get('app.fileConfig');

        $config = array(
            "pathFormat" => $CONFIG['imagePathFormat'],
            "maxSize" => $CONFIG['imageMaxSize'],
            "allowFiles" => $CONFIG['imageAllowFiles']
        );
        $fieldName = 'upfile';

        /* 生成上传实例对象并完成上传 */
        $up = new Uploader($fieldName, $config);

        /* 返回数据 */
        $res = $up->getFileInfo();

        if ($res['state'] == 'SUCCESS') {
            return JsonRespose::success(array('filename' => $res['title'], 'url' => $res['url']));
        } else {
            return JsonRespose::fail($res['state']);
        }
    }

    /**
     * @api {post} /api/common/send-code 发送验证码
     * @apiGroup Wap-Common
     * @apiDescription  说明: 无
     *
     * @apiParam {Int} phone 手机号
     *
     * @apiSuccessExample {json} Success-Response:
     * {
     * "code": 1,
     * "msg": "操作成功!",
     * "data": {
     *    "id": 3
     * }
     * }
     */
    public function postSendCode()
    {
        $phone = Request::get('phone');
        if(!$phone){
            return JsonRespose::fail();
        }
        $res = SmsCodeTable::sendCode($phone);
        return JsonRespose::done($res);
    }

    
}
